package com.googlecode.maratische.google.model;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import be.lechtitseb.google.reader.api.model.exception.GoogleReaderException;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;
import com.googlecode.maratische.google.MainController;

public class CommandSearchSpammedItem  extends Command {
	static Logger logger = Logger.getLogger(CommandRemoveReadedItemMedia.class.getName());

	@Override
	public boolean execute(DaoAction daoAction) throws BaseException, GoogleReaderException, SQLException {
		Spam spam = daoAction.getSpamDao().get(this.getInteger1());
//		System.out.println("SPAM "+spam.getTitle());
		String liteTitle = null;
		switch (spam.getTitleType()) {
		case 1: liteTitle = spam.getTitle();
		case 2: liteTitle = spam.getTitle()+"%";
		default: liteTitle = "%"+spam.getTitle()+"%";
		}
		List<Item> items = daoAction.getItemDao().list("ITEM_READ = 0 AND TITLE like ?", null, liteTitle);
		int count = 0;
		for (Item item : items) {
			++count;
//			System.out.println("item "+item.getTitle());
			Feed feed = daoAction.getFeedDao().get(item.getFeedId());
			MainController.getMainController().markItemAsRead(item, feed);
		}
		if (count>0) {
			spam.setCount(spam.getCount()+count);
			daoAction.getSpamDao().save(spam);
		}
		return true;
	}

	
	@Override
	public String getType() {
		return Commands.SearchSpammedItem.toString();
	}
	
	@Override
	public Integer getPriority() {
		return 8;
	}
}
